﻿using System;
using System.Net;
using System.Web.Mvc;
using Chat.Models.Services;

namespace Chat.Filters{
    public class ApiAuthFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            string token = filterContext.HttpContext.Request.Headers["Token"];

            if (string.IsNullOrEmpty(token))
            {
                // 如果Token不存在，可以执行相应的操作，例如返回未授权的响应
                filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Missing Token");
                return;
            }

            if (global::TokenStore.GetCachedData(token) == null)
            {
                filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Token error");
                return;
            }

            global::TokenStore.ExtendCacheExpiration(token);

        }
    }

}